package cn.com.bluemoon.daps.model.utils.factory.impl.table;

import cn.com.bluemoon.daps.common.enums.DatabaseType;
import cn.com.bluemoon.daps.common.sql.SqlScriptUtils;
import cn.com.bluemoon.daps.model.dto.DapDataModelTableDto;
import cn.com.bluemoon.daps.model.dto.ModelTableInfoDto;
import cn.com.bluemoon.daps.model.dto.SubordinateKeyDto;
import cn.com.bluemoon.daps.model.entity.DapDataModelTable;
import cn.com.bluemoon.daps.model.entity.DapDataModelTableField;
import cn.com.bluemoon.daps.model.entity.DapDataModelTablePartition;
import cn.com.bluemoon.daps.model.utils.contrast.Constant;
import cn.com.bluemoon.daps.model.utils.factory.TableScriptFactory;
import cn.com.bluemoon.daps.model.utils.factory.impl.field.MysqlFieldScript;
import cn.com.bluemoon.daps.model.utils.factory.impl.field.PostgreFieldScript;
import cn.com.bluemoon.daps.model.utils.factory.impl.key.AbsKeyScript;
import cn.com.bluemoon.daps.model.utils.factory.impl.key.MysqlKeyScript;
import cn.com.bluemoon.daps.model.utils.factory.impl.key.PostgreKeyScript;
import com.alibaba.druid.DbType;
import com.google.inject.internal.asm.$TypePath;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.formula.functions.T;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author karry
 * @Data 2021/9/3
 * @Time:14:08
 */
public class PostgreTableScript extends AbsTableScript {

    public final static DbType PGSQL = DbType.postgresql;

    public PostgreTableScript() {
        super(PGSQL);
    }

    @Override
    public String createTableScript(ModelTableInfoDto dto, List<SubordinateKeyDto> keys, List<DapDataModelTablePartition> partitions) {

        String schema = StringUtils.isNotBlank(dto.getTableSchema()) ? dto.getTableSchema() + "." : "";
        String fieldScript = dto.getFieldList().isEmpty() ? "" : postgreFieldScript.createFieldScript(dto.getFieldList());
        AbsKeyScript absKeyScript = new PostgreKeyScript();
        String keyScript = keys.isEmpty() ? "" : absKeyScript.createKeyScript(keys);
        StringBuffer buffer = new StringBuffer().append(fieldScript).append(keyScript);
        String fieldAndKeyScript = buffer.length() > 0 ? buffer.deleteCharAt(buffer.length() - 1).toString() : "";
        StringBuffer tableScript = new StringBuffer(String.format("create table %s%s (%s)", schema, dto.getEngName(), fieldAndKeyScript));
        // 字段以及表注释
        String tableComment = String.format("; COMMENT on table %s%s is '%s';", schema, dto.getEngName(), SqlScriptUtils.formatComment(dto.getName()));
        tableScript.append(tableComment);
        if(!dto.getFieldList().isEmpty()){
            String fieldComment = dto.getFieldList().stream().map(f -> String.format("comment on column %s%s.%s is '%s'",schema, dto.getEngName(),
                    f.getFieldName(), SqlScriptUtils.formatComment(f.getComment()))).collect(Collectors.joining(";"));
            tableScript.append(fieldComment).append(";");
        }
        return tableScript.toString();
    }

    @Override
    public void modifyTableScript(DapDataModelTableDto n, DapDataModelTableDto o,
                                  String tableSchema, Map<String, String> map) {
        String schema = StringUtils.isNotBlank(n.getTableSchema()) ? n.getTableSchema() + "." : "";
        // 查询英文表名是否发生修改
        if (!StringUtils.equalsIgnoreCase(n.getEngName(), o.getEngName())) {
            map.put(Constant.MT, String.format("%s%s%s rename to %s;", Constant.AT, schema, o.getEngName(), n.getEngName()));
        }
        // 查询中文表名是否发生修改
        if (!n.getName().equals(o.getName())) {
            map.put(Constant.MC, String.format("comment on %s%s is '%s';", schema, n.getEngName(), n.getName()));
        }
    }

    @Override
    public void deleteTableScript(String tableName, String tableSchema, Map<String, String> map) {

        String schema = StringUtils.isNotBlank(tableSchema) ? tableSchema + "." : "";
        String dropScript = String.format("%s%s%s;", Constant.DTA, schema, tableName);
        map.put(Constant.DT, dropScript);
    }

}
